package dm71.espil.activities;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import dm71.espil.R;
import dm71.espil.REST.DataService;
import dm71.espil.REST.Tasks.LoginTask;
import dm71.espil.REST.LoginParameters;

public class LoginActivity extends Activity
{
	Button btnLogin;
	Button btnCreateProfile;
	EditText txtUsername;
	EditText txtPassword;
	TextView lblLoginStatus;
	LoginActivity context = this;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		btnLogin = (Button)findViewById(R.id.btnLogin);
		txtUsername = (EditText)findViewById(R.id.txtUsername);
		txtPassword = (EditText)findViewById(R.id.txtPassword);
		lblLoginStatus = (TextView)findViewById(R.id.lblLoginStatus);		
		
		btnCreateProfile = (Button)findViewById(R.id.btnCreateProfile);
		btnCreateProfile.setOnClickListener(new View.OnClickListener() 
		{	
			public void onClick(View v)
			{
				Intent intent = new Intent(context, OpretProfilActivity.class);
		    	startActivity(intent);
			}
		});
		
		btnLogin.setOnClickListener(new View.OnClickListener() 
		{
			public void onClick(View v)
			{
				LoginTask loginTask = new LoginTask(context);
				loginTask.execute(new LoginParameters(txtUsername.getText().toString(), txtPassword.getText().toString()));
			}
		});
	}

	/**
	 * This method gets invoked by the LoginTask, after it is done calling the webservice from a background thread.
	 *
	 * @param result
	 */
	public void LogInComplete(Boolean result)
	{
	    if (result)
	    {
	    	finish();
	    	Intent intent = new Intent(context, MainMenuActivity.class);
	    	startActivity(intent);
	    }
	    else
	    {
	    	Resources res = getResources();
	    	String loginFailedText = res.getResourceName(R.string.loginfailed);
	    	lblLoginStatus.setText(loginFailedText);
	    }
	}
}
